; (function (window, $) {
  // 获得需要跳转回去的uri
  var gotoUrl = util.getQuery().redirect_uri
  console.log('redirct_uri', gotoUrl)

  $(function () {
    $('#two').hide()
    //tab切换
    var tabs = $('.tabs li a')

    tabs.click(function () {
      var content = this.hash.replace('/', '')
      tabs.removeClass('active')
      $(this).addClass('active')
      $('#content')
        .find('.content-wrap')
        .hide()
      $(content).fadeIn(200)
    })

    // login btn click
    $('#login-btn').on('click', login)
    $(document).on('keydown', function () {
      if (event.keyCode === 13) {
        login()
      }
    })
  })

  function login() {
    var loginDom = $('#login-username')
    var pwdDom = $('#login-pwd')

    // empty string check
    if (loginDom.val() === '') {
      loginDom.css('border-color', '#FF0000')
      return
    } else if (loginDom.val().length < 11) {
      layer.alert('请输入正确的手机号码', {
        skin: 'demo-class',
        closeBtn: 0
      });
      return
    }

    if (pwdDom.val() === '') {
      pwdDom.css('border-color', '#FF0000')
      return
    }

    // before login,clear localStorage,do not use localStroage={}
    localStorage.clear()
    Cookies.remove('Authorization', { path: '/' });

    util.ajax({
      url: '/auth',
      type: 'POST',
      data: {
        username: loginDom.val(),
        password: pwdDom.val()
      },
      success: function (rsp) {
        /**
         * @param sub login username
         * @param iat login timestamp
         * @param exp expire timestamp
         *
         *  in javascript the timestamp is ms,you should x1000 for java timestamp
         *
         */
        var res = jwt_decode(rsp.token)
        res.exp = res.exp * 1000
        res.iat = res.iat * 1000
        res.token = rsp.token
        for (var item in res) {
          localStorage.setItem(item, res[item])
        }
        var reqUrl = rsp.userType === 1 ? '/user/member' : '/user/manager'

        util.ajax({
          url: reqUrl,
          type: 'GET',
          headers: {
            Authorization: 'Bearer ' + localStorage.token
          },
          success: function (res) {
            // save userinfo into localstorage
            for (var item in res.data) {
              if (res.data[item] === null) {
                res.data[item] = '';
              }
              localStorage.setItem(item, res.data[item])
            }

            var userType = parseInt(localStorage.userType)
            // back to some url
            if (!gotoUrl) {
              gotoUrl = '/index.html'
            } else {
              if (userType === 1 && !MemberCanGo(gotoUrl)) {
                gotoUrl = '/index.html'
              }

              if (userType === 2 && !ManagerCanGo(gotoUrl)) {
                gotoUrl = '/index.html'
              }
            }

            window.location.href = gotoUrl
          }
        })
      },
      error: function (err) {
        var code = err.status
        console.log(code)
        layer.alert('登录失败，用户名或密码错误', {
          skin: 'demo-class',
          closeBtn: 0
        })
        // alert('登录失败，用户名或密码错误');
      }
    })
  }

  function MemberCanGo(gotoUrl) {
    var urls = ['/channel']
    for (var i = 0, item; (item = urls[i++]);) {
      if (gotoUrl.indexOf(item) !== -1) {
        return false
      }
    }
    return true
  }

  function ManagerCanGo(gotoUrl) {
    var urls = ['/my', '/assess/pro']
    for (var i = 0, item; (item = urls[i++]);) {
      if (gotoUrl.indexOf(item) !== -1) {
        return false
      }
    }
    return true
  }
})(window, jQuery)
